package cn.edu.zjicm.wordsnet_d.fragment;

import java.util.Random;

import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.ExamRunActivity;
import cn.edu.zjicm.wordsnet_d.activity.LeaderboardActivity;
import cn.edu.zjicm.wordsnet_d.data.MobclickAgentData;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.download.JudgeNetwork;
import cn.edu.zjicm.wordsnet_d.synchronize.UserInterface;
import cn.edu.zjicm.wordsnet_d.util.ShareUtil;
import cn.edu.zjicm.wordsnet_d.util.Utils;
import cn.edu.zjicm.wordsnet_d.view.CreateShareBitmap;
import cn.edu.zjicm.wordsnet_d.view.OnRiseCompleteListener;
import cn.edu.zjicm.wordsnet_d.view.RiseNumTextView;
import cn.edu.zjicm.wordsnet_d.view.ShareDialog;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 打卡
 * 
 * @author cnting
 * 
 */
public class PunchOutFragment extends Fragment implements OnClickListener, OnRiseCompleteListener {

	private View mView;
	private RiseNumTextView punchOutTv;
	private ExamRunActivity mExamRunActivity;
	private ImageView shareQQ, shareWeixin, shareSina;
	private ShareDialog shareDialog;
	private ImageView punchOutFinishImg, tickImg;
	private LinearLayout shareLayout;
	private TextView finishBtn;
	private TextView leaderBoard;

	private int punchOutTimes = 0;
	private final static int START_ANIMATION = 0;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case START_ANIMATION:
				startAnimation();
				break;
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_punch_out, null);
		mExamRunActivity = (ExamRunActivity) getActivity();
		findView();
		initView();
		return mView;
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	private void initView() {
		punchOutTimes = WordFactory.getInstance(mExamRunActivity).getPunchOutTimes();
		punchOutTv.setValue(punchOutTimes, this);
		shareQQ.setOnClickListener(this);
		shareWeixin.setOnClickListener(this);
		shareSina.setOnClickListener(this);
		mView.findViewById(R.id.punch_out_share_qq_zone).setOnClickListener(this);
		mView.findViewById(R.id.punch_out_share_friend).setOnClickListener(this);
		leaderBoard.setOnClickListener(this);

		handler.sendEmptyMessageDelayed(START_ANIMATION, 500);
		finishBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mExamRunActivity.finish();
			}
		});
	}

	private void startAnimation() {
		Animation animation = AnimationUtils.loadAnimation(mExamRunActivity, R.anim.punch_out_tick_animation);
		tickImg.setAnimation(animation);
		animation.start();
		tickImg.setVisibility(View.VISIBLE);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation arg0) {
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				tickImg.clearAnimation();
				punchOutTv.startRise();
			}
		});
	}

	private void startAnimation2() {
		punchOutTv.clearAnimation();
		Animation animation = AnimationUtils.loadAnimation(mExamRunActivity, R.anim.num_animation);
		punchOutTv.setAnimation(animation);
		animation.start();
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation arg0) {
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				Animation animation2 = AnimationUtils.loadAnimation(mExamRunActivity, R.anim.translate_from_bottom);
				shareLayout.setAnimation(animation2);
				animation2.start();
				shareLayout.setVisibility(View.VISIBLE);
			}
		});
	}

	private void findView() {
		punchOutTv = (RiseNumTextView) mView.findViewById(R.id.punch_out_times);
		shareQQ = (ImageView) mView.findViewById(R.id.punch_out_share_qq);
		shareWeixin = (ImageView) mView.findViewById(R.id.punch_out_share_weixin);
		shareSina = (ImageView) mView.findViewById(R.id.punch_out_share_sina);
		punchOutFinishImg = (ImageView) mView.findViewById(R.id.punch_out_finish_img);
		tickImg = (ImageView) mView.findViewById(R.id.tick_img);
		shareLayout = (LinearLayout) mView.findViewById(R.id.share_layout);
		finishBtn = (TextView) mView.findViewById(R.id.finish_punch);
		leaderBoard = (TextView) mView.findViewById(R.id.leaderBoard);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.add_school_btn:
			if (JudgeNetwork.getInstance(mExamRunActivity).isWifiConnected() || JudgeNetwork.getInstance(mExamRunActivity).isMobileConnected()) {
				LeaderboardActivity.startActivity(mExamRunActivity);
			} else {
				Toast.makeText(mExamRunActivity, UserInterface.MSG_BAD_NETWORK_STRING, Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.punch_out_share_qq:// QQ
			initShareContent(Utils.shareWay.QQ);
			break;
		case R.id.punch_out_share_qq_zone:// QQ空间
			initShareContent(Utils.shareWay.QZone);
			break;
		case R.id.punch_out_share_weixin:// 微信
			initShareContent(Utils.shareWay.wechat);
			break;
		case R.id.punch_out_share_friend:// 微信朋友圈
			initShareContent(Utils.shareWay.wechatTimeline);
			break;
		case R.id.punch_out_share_sina:
			initShareContent(Utils.shareWay.weibo);
			break;
		case R.id.leaderBoard:
			LeaderboardActivity.startActivity(mExamRunActivity);
			break;
		}
	}

	private void initShareContent(Utils.shareWay way) {
		Activity context = getActivity();
		int dayPunchOuted = WordFactory.getInstance(context).getPunchOutTimes();
		int numsTodayLearned = AppPreference.getTodayStudyNewWordCount(context) + AppPreference.getTodayReviewWordCount(context);

		String shareTitle = "打卡当学霸";
		String shareTargetUrl = "http://www.iwordnet.com/mobile/m.share.php";
		Bitmap shareBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.share_logo);
		String imageURL = "http://www.iwordnet.com/mobile/image/share_logo.jpg";
		String shareContent = "第" + dayPunchOuted + "天打卡，学习了" + numsTodayLearned + "个单词，获得了" + AppPreference.getEXP(context) + "经验值，我们一起刷单词吧！";

		String nickname = AppPreference.getNickName(context);
		if (nickname == null) {
			nickname = AppPreference.getLoginID(context);
		}
		if (nickname.equals("guest") || nickname.equals("")) {
			nickname = "Guest";
		}
		shareTargetUrl += "?str=";
		shareTargetUrl += nickname;
		shareTargetUrl += "%23";
		shareTargetUrl += dayPunchOuted;
		shareTargetUrl += "%23";
		shareTargetUrl += numsTodayLearned;
		shareTargetUrl += "%23";
		shareTargetUrl += AppPreference.getToken(context);
		shareTargetUrl += "%23";
		if (AppPreference.getHeadPortrait(context) == null) {
			shareTargetUrl += "0";// 用户没有设置头像
		} else {
			shareTargetUrl += "1";// 用户设置了头像
		}
		shareTargetUrl = Utils.changeCharsetToUTF8(shareTargetUrl);

		switch (way) {
		case weibo:
			MobclickAgentData.share(context, "打卡分享--微博");
			shareTargetUrl = "http://t.cn/RzZc3Tt";
			String randomContent[] = { "以前我总是记不住单词，绝望是那么的触手可及，但是现在，我是傲视一切的单词小英雄，我感觉人生巅峰已离我不远了。你也想吗，那就一起背单词吧。", "我背单词从来都不是为了炫耀，我只希望自己能徜徉在英文经典的海洋，漫步在异国他乡的街头，让自己的心得到升华。我们一起背单词吧。",
					"我一直在坚持，只为心中的梦想，I am a slow walker，but I never walk backwards ，fighting! ", "The first step is as good as half over。第一步是最关键的一步，迈出了关键的一步，就已经成功了一半！我们一起背单词吧。" };
			shareContent = "我正在使用#知米背单词#学习，今天是第" + dayPunchOuted + "天打卡，学习了" + numsTodayLearned + "个单词，获得了" + AppPreference.getEXP(context) + "经验值。"
					+ randomContent[(int) (Math.random() * randomContent.length)] + "@知米英语";
			shareBitmap = Utils.getBitmapFromView(new CreateShareBitmap(context, Utils.createBitmapMode.forPunchout).getView(), context);
			break;
		case QQ:
			MobclickAgentData.share(context, "打卡分享--QQ");
			break;
		case QZone:
			MobclickAgentData.share(context, "打卡分享--QQ空间");
			break;
		case wechat:
			MobclickAgentData.share(context, "打卡分享--微信");
			shareContent = "第" + dayPunchOuted + "天打卡，学习了" + numsTodayLearned + "个单词，获得了" + AppPreference.getEXP(context) + "经验值，我们一起刷单词吧！";

			break;
		case wechatTimeline:
			MobclickAgentData.share(context, "打卡分享--微信朋友圈");
			shareContent = "第" + dayPunchOuted + "天打卡，学习了" + numsTodayLearned + "个单词，获得了" + AppPreference.getEXP(context) + "经验值，我们一起刷单词吧！";

			break;
		}

		new ShareUtil(context, way, shareBitmap, imageURL, shareTitle, shareContent, shareTargetUrl);
	}

	@Override
	public void onRiseCompleted() {
		startAnimation2();
	}
}
